package df.bean.process;

/**
 *
 * @author T.
 */
import df.bean.db.conn.DBConn;
import df.bean.db.conn.DBConnection;
import df.bean.obj.util.Variables;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ProcessJoinBill {

    private String startDate = "";
    private String toDate = "";
    private String hospital_code = "";
    private DBConnection con;

    private String getStartDate() {
        return startDate;
    }

    private String getToDate() {
        return toDate;
    }

    private void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    private void setToDate(String toDate) {
        this.toDate = toDate;
    }

    private void getBillByLineNO(String idRef, String condition){
        String sql = "SELECT BILL_NO, LINE_NO, AMOUNT_BEF_DISCOUNT, TRANSACTION_TYPE " +
                	 "FROM INT_HIS_BILL WHERE HOSPITAL_CODE = '"+this.hospital_code+"' " +
                	 "AND LINE_NO = '"+ idRef +"'";
        Double temp_sum = 0.0;
        String temp_line = "";
        String temp_bill = "";
        String sql_insert = "";
		Statement stmt = null;
        ResultSet rs = null;
        try{
    		stmt = con.getConnection().createStatement();
            rs = stmt.executeQuery(sql);
            int iCount =0;
            while(rs.next()){
            	if(condition.equals("CR")){
                    if("INV".equalsIgnoreCase(rs.getString("TRANSACTION_TYPE")) || iCount==0){
                        temp_line = rs.getString("LINE_NO");
                        temp_bill = rs.getString("BILL_NO");
                        iCount++;
                    }
            	}else{
                    if("REV".equalsIgnoreCase(rs.getString("TRANSACTION_TYPE")) || iCount==0){
                        temp_line = rs.getString("LINE_NO");
                        temp_bill = rs.getString("BILL_NO");
                        iCount++;
                    }
            	}
                temp_sum = temp_sum + Double.parseDouble(rs.getString("AMOUNT_BEF_DISCOUNT"));                
            }
            sql_insert += "update INT_HIS_BILL set AMOUNT_BEF_DISCOUNT='0.0' where HOSPITAL_CODE = '"+
            			  this.hospital_code+"' AND LINE_NO='"+ temp_line +"'; ";
            sql_insert += "update INT_HIS_BILL set AMOUNT_BEF_DISCOUNT='"+ temp_sum +"' where HOSPITAL_CODE = '"+
            			  this.hospital_code+"' and LINE_NO='"+ temp_line +"' and BILL_NO='"+ temp_bill +"'";
            con.executeUpdate(sql_insert);
        }catch(Exception err){
            System.out.println(err.getMessage());
        }finally{
            try{
            	stmt.close();
            	rs.close();
            }catch(Exception err){
            }
        }
        //ResultSet rs =
    }
    private ResultSet getLineNO(){
    	Statement stmt = null;
    	ResultSet rs = null;
        String sql = "SELECT LINE_NO " +
                "FROM INT_HIS_BILL " +
                "WHERE AMOUNT_BEF_DISCOUNT > 0 " +
                "AND HOSPITAL_CODE = '"+this.hospital_code+"' "+
                "AND BILL_DATE BETWEEN '"+this.getStartDate()+"' AND '"+this.getToDate()+"' " +
                "GROUP BY LINE_NO HAVING COUNT(LINE_NO) > 1;";
        try{
			stmt = con.getConnection().createStatement();
            rs = stmt.executeQuery(sql);
        }catch(Exception err){
            System.out.println(err.getMessage());
        }
        return rs;
    }
    public boolean ProcessJoinBill(String hospital_code, String startDate, String toDate){
    	String condition = "";
    	this.con = new DBConnection();
        this.con.connectToLocal();
        this.setStartDate(startDate);
        this.setToDate(toDate);
        this.hospital_code = hospital_code;
        boolean status = true;
        ResultSet rs = null;
        int count = 0;
        try{
        	condition = con.executeQueryString("SELECT JOIN_BILL FROM HOSPITAL WHERE CODE = '"+hospital_code+"'").toString();
            con.executeUpdate("UPDATE INT_HIS_BILL SET OLD_AMOUNT = AMOUNT_BEF_DISCOUNT " +
    	            "WHERE HOSPITAL_CODE = '"+hospital_code+"' " +
    	            "AND TRANSACTION_DATE BETWEEN '"+startDate+"' AND '"+toDate+"'");
            if(!condition.equals("NO")){	
	    		rs = this.getLineNO();
	            while(rs.next()){
	                this.getBillByLineNO(rs.getString("LINE_NO"),condition);
	                count++;
	            }
	            rs.close();
        	}
        	con.Close();
        }catch(Exception err){
            if(Variables.IS_TEST){
            	System.out.println("Error on process join bill : "+err.getMessage());
            }
            status = false;
        }
        return status;
    }
}
